package JUC;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


class TestJoin {
    public static void main(String[] args) throws InterruptedException {
        Ticket ticket = new Ticket();
        new Thread(() -> {
            for (int i = 1; i <= 40; i++) ticket.saleTicket();
        }, "A").start();
        new Thread(() -> {
            for (int i = 1; i <= 40; i++) ticket.saleTicket();
        }, "B").start();
        new Thread(() -> {
            for (int i = 1; i <= 40; i++) ticket.saleTicket();
        }, "C").start();
    }
}

class Ticket {
    Lock lock = new ReentrantLock();
    private int number = 40;

    public void saleTicket() {
        lock.lock();
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出第 " + (number--) + "票,还剩下:" + number);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}